package com.cn.boot.spring.service.impl;

import com.cn.boot.spring.entity.WxPermissionEntity;
import com.cn.boot.spring.entity.WxUserEntity;
import com.cn.boot.spring.service.IWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service("userDetailsService")
public class MyUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private IWxUserService iWxUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        WxUserEntity user= iWxUserService.getUserByName(username);
        List<WxPermissionEntity> permissionList= iWxUserService.getPermissionsByUserId(user.getId());
        HashSet<WxPermissionEntity> permissions = new HashSet<>(permissionList);
        user.setAuthorities(permissions);
        return user;

    }
}
